package com.lzb.compressmaster.activity;

import android.text.InputType;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.lzb.compressmaster.BaseAc;
import com.lzb.compressmaster.Content;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.basic.utils.SPUtil;
import com.lzb.compressmaster.databinding.ActivityInputPwdBinding;

public class InputPwdActivity extends BaseAc<ActivityInputPwdBinding> implements View.OnClickListener {

    private boolean isDisplay;
    private boolean isNeed;

    @Override
    protected int onCreate() {
        return R.layout.activity_input_pwd;
    }

    @Override
    protected void initView() {
        mDataBinding.ivInputPwdBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.ivDisplayPassword.setOnClickListener(this);
        mDataBinding.llForgetPassword.setOnClickListener(this);
        mDataBinding.ivExitNeedPassword.setOnClickListener(this);
        mDataBinding.llChangePassword.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ivDisplayPassword) {//显示密码
            if (isDisplay) {
                isDisplay = false;
                mDataBinding.ivDisplayPassword.setImageResource(R.drawable.awxz);
                mDataBinding.etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                mDataBinding.etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                mDataBinding.etPassword.setSelection(mDataBinding.etPassword.getText().length());
            } else {
                isDisplay = true;
                mDataBinding.ivDisplayPassword.setImageResource(R.drawable.axuanz);
                mDataBinding.etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                mDataBinding.etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                mDataBinding.etPassword.setSelection(mDataBinding.etPassword.getText().length());
            }
        } else if (id == R.id.llForgetPassword) {//忘记密码
            startActivity(ForgetPasswordActivity.class);
        } else if (id == R.id.ivExitNeedPassword) {//退出后再次进入需要密码
            if (isNeed) {
                isNeed = false;
                mDataBinding.ivExitNeedPassword.setImageResource(R.drawable.awxz);
            } else {
                isNeed = true;
                mDataBinding.ivExitNeedPassword.setImageResource(R.drawable.axuanz);
            }
            SPUtil.putBoolean(mContext, Content.isNeedPassword, isNeed);
        } else if (id == R.id.llChangePassword) {//修改密码
            startActivity(ChangePwdActivity.class);
        } else if (id == R.id.tvConfirm) {//确定
            String inputPassword = mDataBinding.etPassword.getText().toString().trim();
            if (TextUtils.isEmpty(inputPassword)) {
                ToastUtils.showShort(getString(R.string.please_input_password));
                return;
            }
            String password = SPUtil.getString(mContext, Content.myPassword, "");
            if (!inputPassword.equals(password)) {
                ToastUtils.showShort(getString(R.string.password_error));
                return;
            }
            startActivity(PrivacyFileActivity.class);
        }
    }

    @Override
    protected void initData() {
        isNeed = SPUtil.getBoolean(mContext, Content.isNeedPassword, true);
        if (isNeed) {
            mDataBinding.ivExitNeedPassword.setImageResource(R.drawable.axuanz);
        } else {
            mDataBinding.ivExitNeedPassword.setImageResource(R.drawable.awxz);
        }

    }
}